<?php
class AdminPrestathemeCmsController extends ModuleAdminController
{
	public function __construct()
	{
		$this->table      = 'pt_cms';
		$this->identifier = 'id_cms';
		$this->className  = 'PrestathemeCms';
		$this->lang       = true;
		
		$this->context = Context::getContext();
		
		$this->addRowAction('view');
		$this->addRowAction('edit');
		$this->addRowAction('delete');
		
		$this->bulk_actions = array(
			'delete' => array(
				'text' => $this->l('Delete selected'), 
				'confirm' => $this->l('Delete selected items?')
			)
		);

		$this->fields_list = array(
			'id_cms' => array(
				'title' => $this->l('ID'), 
				'align' => 'center', 
				'width' => 25
			),
			'code' => array(
				'title' => $this->l('Name'),
				'width' => 'auto'
			),
			'version' => array(
				'title' => $this->l('Version'),
				'width' => 'auto'
			),
			'url' => array(
				'title' => $this->l('Link'), 
				'width' => 'auto'
			),
			'active' => array(
				'title' => $this->l('Displayed'), 
				'width' => 25, 
				'align' => 'center', 
				'active' => 'status', 
				'type' => 'bool', 
				'orderby' => false
			)
		);		
		parent::__construct();
	}
	
	public function renderForm()
	{
		$this->fields_form = array(
			'tinymce' => true,
			'legend' => array(
				'title' => $this->l('Available CMS for prestatheme'),
				'image' => '../img/admin/cog.gif'
			),
			'input' => array(
				array(
					'type' => 'text',
					'label' => $this->l('Full Name'),
					'name' => 'name',
					'size' => 33,
					'required' => true,
					'lang' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'desc' => $this->l('The full name of the CMS, as PrestaShop™ , Magento, eCommerce etc...')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Short Name'),
					'name' => 'code',
					'size' => 33,
					'required' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('The short name of the CMS used while qiven the version, as prestashop')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Version'),
					'name' => 'version',
					'size' => 33,
					'required' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('The CMS version. Format: x.y.z where x, y and z are integer')
				),
				array(
					'type' => 'text',
					'label' => $this->l('URL Link'),
					'name' => 'url',
					'size' => 33,
					'required' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('The public link to access the default CMS installation.')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Default Path'),
					'name' => 'path',
					'size' => 33,
					'required' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('The server path of the default theme of the CMS.') .'<br />'. sprintf($this->l('Base directory: %s'), realpath(_PS_MODULE_DIR_))
				),
				array(
					'type' => 'textarea',
					'label' => $this->l('Description'),
					'name' => 'description',
					'autoload_rte' => true,
					'lang' => true,
					'rows' => 5,
					'cols' => 40,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('About the CMS ...')
				),
				array(
					'type' => 'radio',
					'label' => $this->l('Available'),
					'name' => 'active',
					'required' => false,
					'class' => 't',
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
						),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
						)
					),
				),
				array(
					'type' => 'text',
					'label' => $this->l('Iso Lang'),
					'name' => 'lang_iso',
					'size' => 33,
					'readonly' => true,
					'required' => true,
					'lang' => true,
					'desc' => $this->l('This field is authomaticaly fill.'),
				),
			),
			'submit' => array(
				'title' => $this->l('   Save   '),
				'class' => 'button'
			)
		);
        $languages = Language::getLanguages(false);
        $this->fields_value['lang_iso'] = array();
        foreach ($languages AS $lang)
        {
            $this->fields_value['lang_iso'][$lang['id_lang']] = $lang['iso_code'];
        }

		if (!($obj = $this->loadObject(true)))
			return;

		return parent::renderForm();
	}

	public function renderView()
	{
		if (!($object = $this->loadObject()))
			return;
			
		$this->tpl_view_vars = array(
			'id_lang' => Context::getContext()->language->id,
			'object' => $object,
			'head_title' => ($object->id ? $object->getCMSName() : $this->l('New CMS to be avaible on Prestatheme')),
			'shopContext' => Shop::getContext(),
		);

		return parent::renderView();
	}
}